package com.youdian.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.youdian.R;
import com.youdian.adapter.BusinessAdapter;
import com.youdian.adapter.FoodsAdapter;
import com.youdian.entity.BaseEntity;
import com.youdian.entity.Business;
import com.youdian.entity.Food;
import com.youdian.http.BaseObserver;
import com.youdian.http.RetrofitFactory;
import com.youdian.http.RxSchedulers;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by moker on 2017/12/14.
 */



public class FoodsActivity extends AppCompatActivity implements XRecyclerView.LoadingListener{


    @BindView(R.id.recycler_view)
    XRecyclerView mRecyclerView;
    private FoodsAdapter mBAdapter;
    private List<Food> mBData = new ArrayList<>();

    private int businessId;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_foods);
        ButterKnife.bind(this);
        businessId = getIntent().getIntExtra("businessId",-1);
        //LinearLayoutManager是线性布局，setOrientation可以设置他的方向是横向还是纵向。
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mBAdapter=new FoodsAdapter(mBData,this);
        mRecyclerView.setAdapter(mBAdapter);
        mRecyclerView.setLoadingListener(this);
        mRecyclerView.setLoadingMoreEnabled(true);
        loadData(businessId);
    }



    public void loadData(int businessId){
        RetrofitFactory.getInstance().getFoodsList(businessId)
                .compose(RxSchedulers.<BaseEntity<List<Food>>>compose())
                .subscribe(new BaseObserver<List<Food>>(FoodsActivity.this) {
                    @Override
                    protected void onHandleSuccess(List<Food> foods) {
                        Log.i("Food",foods.size()+"");
                        mBData.clear();
                        mBData.addAll(foods);
                        mBAdapter.notifyDataSetChanged();
                        mRecyclerView.refreshComplete();
                    }
                });
    }

    @Override
    public void onRefresh() {
        loadData(businessId);
    }

    @Override
    public void onLoadMore() {

    }
}
